Skip to content

test(api): bring the vector factory tests under strict pyrefly - #46368

Open
MindOfqc wants to merge 7 commits into
langgenius:mainfrom
MindOfqc:test/pyrefly-vector-factory
Open

MindOfqc wants to merge 7 commits into
langgenius:mainfrom
MindOfqc:test/pyrefly-vector-factory

Conversation

@MindOfqc

Copy link
Copy Markdown
Contributor

What

Removes core/rag/datasource/vdb/test_vector_factory.py from the strict-mode exclusion list in api/tests/unit_tests/pyrefly.toml and fixes the 68 diagnostics it was hiding.

How

  • The vector_factory_module fixture returns importlib.reload(...), so it is annotated types.ModuleType.
  • The _dataset helper keeps its **overrides flexibility but constructs the record through Dataset(...) and applies the remaining keys with setattr; Dataset(**values) produced 13 bad-argument-type diagnostics because the SQLAlchemy constructor is not statically typed for arbitrary string keys.
  • The persisted index_struct column stays writable while index_struct_dict is a read-only property, so the helper keeps translating the dict form into the column before construction.
  • The vector stub gets an explicit keyword-only signature, and _attributes is assigned list[str]() rather than annotated (assigning to a non-self attribute cannot carry an annotation).

No test logic, assertion or expected value was changed.

Verification

pyrefly (whole project, tests/unit_tests/pyrefly.toml): 29 errors, all missing-import; 0 non-import errors; 0 warnings
pytest tests/unit_tests/core/rag/datasource/vdb/test_vector_factory.py: 31 failed, 24 passed — byte-identical to the pre-change baseline (the failures come from this machine only having the clickzetta vector backend installed: "Vector store <VectorType.CHROMA> is not supported. Installed backends: clickzetta")
ruff check / format --check: clean

No # pyrefly: ignore, no # type: ignore, no typing.cast, no Any.

Type the module fixture as a module, give the dataset helper an explicit
construction path and annotate the vector stub, then drop the file from the
strict-mode exclusion list.
@github-actions github-actions Bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Nov 14, 2026
@github-actions

github-actions Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 87.16% 87.21% +0.05%
Strict coverage 86.82% 86.87% +0.05%
Typed symbols 96,578 96,633 +55
Untyped symbols 14,280 14,229 -51
Modules 4652 4652 0

@codecov

codecov Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.30%. Comparing base (296abf4) to head (f44ad12).
⚠️ Report is 86 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff            @@
##             main   #46368    +/-   ##
========================================
  Coverage   90.30%   90.30%            
========================================
  Files        6493     6496     +3     
  Lines      408263   408617   +354     
  Branches    81859    81893    +34     
========================================
+ Hits       368670   369017   +347     
- Misses      33189    33199    +10     
+ Partials     6404     6401     -3     
Flag Coverage Δ
api 90.14% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@asukaminato0721 asukaminato0721 self-assigned this Nov 15, 2026
`AbstractVectorFactory.init_vector` and `Vector.__init__` declared
`attributes` as a bare `list`, so every caller and override had to treat
the entries as `Any`. The list only ever carries metadata field names, so
it is declared as `list[str]`, and `Dataset.index_struct_dict` gets the
`dict[str, Any] | None` it actually returns.

With those types in place the vector factory test no longer needs to build
its dataset by `setattr`-ing untyped overrides onto an instance: the helper
takes the two fields the tests actually vary as real parameters.

@asukaminato0721 asukaminato0721 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 inline suggestions at 3b5d7f585b, each from a rule learned from earlier reviews in this repository. Reply on a comment if it is wrong.

dify-codereview

@github-actions github-actions Bot added size:XXL This PR changes 1000+ lines, ignoring generated files. and removed size:L This PR changes 100-499 lines, ignoring generated files. labels Nov 16, 2026
@github-actions github-actions Bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:XXL This PR changes 1000+ lines, ignoring generated files. labels Nov 16, 2026
asukaminato0721

This comment was marked as outdated.

@asukaminato0721 asukaminato0721 added the waiting-for-reviewer dify-codereview: checked, nothing left for the author; a reviewer's turn label Nov 16, 2026
vector_factory_module: types.ModuleType, caplog: pytest.LogCaptureFixture
) -> None:
"""Progress logs must report the batch count, not a text count with the raw remainder."""
vector = vector_factory_module.Vector.__new__(vector_factory_module.Vector)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

avoid __new__ here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in d1e57ad8. The progress test now constructs a real Vector, uses a real SQLAlchemy session and production resolve_vector_type/factory path, and no longer uses Vector.__new__ or patches the factory. It asserts the externally visible 1/2 and 2/2 progress logs and processor batch sizes.

def test_create_skips_empty_text_documents_before_embedding(vector_factory_module):
def test_create_skips_empty_text_documents_before_embedding(vector_factory_module: types.ModuleType) -> None:
vector = vector_factory_module.Vector.__new__(vector_factory_module.Vector)
vector._embeddings = MagicMock()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.

def test_add_texts_with_optional_duplicate_check(vector_factory_module):
def test_add_texts_with_optional_duplicate_check(vector_factory_module: types.ModuleType) -> None:
vector = vector_factory_module.Vector.__new__(vector_factory_module.Vector)
vector._embeddings = MagicMock()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.


def test_add_texts_filters_empty_documents_before_duplicate_check(vector_factory_module):
def test_add_texts_filters_empty_documents_before_duplicate_check(vector_factory_module: types.ModuleType) -> None:
vector = vector_factory_module.Vector.__new__(vector_factory_module.Vector)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.

def test_filter_duplicate_texts_and_getattr(vector_factory_module):
def test_filter_duplicate_texts_and_getattr(vector_factory_module: types.ModuleType) -> None:
vector = vector_factory_module.Vector.__new__(vector_factory_module.Vector)
vector.text_exists = MagicMock(side_effect=lambda doc_id: doc_id == "dup")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.

@asukaminato0721 asukaminato0721 added waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address and removed waiting-for-reviewer dify-codereview: checked, nothing left for the author; a reviewer's turn labels Nov 16, 2026
asukaminato0721

This comment was marked as outdated.

@asukaminato0721 asukaminato0721 removed the waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address label Nov 17, 2026
@asukaminato0721 asukaminato0721 added the waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address label Nov 17, 2026

@asukaminato0721 asukaminato0721 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI is failing: Style Check / Python Style. Please fix it. 1 inline suggestion at 8599680381, from a rule learned from earlier reviews in this repository. Reply on the comment if it is wrong. 5 review threads are still unresolved.

dify-codereview

@asukaminato0721 asukaminato0721 added waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address and removed waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address labels Nov 20, 2026

@asukaminato0721 asukaminato0721 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 inline suggestion at d1e57ad8a1, from a rule learned from earlier reviews in this repository. Reply on the comment if it is wrong. 5 review threads are still unresolved.

dify-codereview

return [[0.1] for _ in texts]

@override
def embed_multimodal_documents(self, multimodel_documents: list[dict[str, object]]) -> list[list[float]]:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parse, don't validate (https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/): model this data once at the boundary as a pydantic model / dataclass / TypedDict (Model.model_validate(raw)), and pass the typed value on, instead of passing dicts around and re-checking keys and types where they are used. In a test, don't build this as an ad hoc dict[str, object]: define the shape (TypedDict / model) in the production code and import it here.

dify-codereview · parse-dont-validate Learned from an earlier review. Reply here if this is wrong.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an override of the production Embeddings interface, whose multimodal inputs are open dictionaries. This test fake does not parse the mapping or inspect any keys; it returns one stable vector per input. The test is for vector batch progress, so I’m keeping the override compatible with the existing interface rather than adding a production schema here.

@asukaminato0721 asukaminato0721 added waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address and removed waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address labels Nov 21, 2026

def test_create_skips_empty_text_documents_before_embedding(vector_factory_module):
def test_create_skips_empty_text_documents_before_embedding(vector_factory_module: types.ModuleType) -> None:
vector = vector_factory_module.Vector.__new__(vector_factory_module.Vector)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no new

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in f44ad12: this test now constructs a real Vector with a SQLAlchemy session and typed recording adapters. It still checks that empty and whitespace-only input never reaches embedding or the backend. All 55 vector factory unit tests passed, and strict Pyrefly reported zero diagnostics for the changed file.

@asukaminato0721 asukaminato0721 added waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address and removed waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address labels Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L This PR changes 100-499 lines, ignoring generated files. waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants